﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Camping_Project.Forms
{
    public partial class FormDates : Form
    {

        //Attributs
        bool type;

        public FormDates(bool type)
        {
            InitializeComponent();
            this.type = type;
            MessageBox.Show("" + type);
        }

        //Attributs
        private int identifiant = 0;

        private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {
            dataGridView1.Rows.Clear();
            groupBox1.Show();
            DateTime dateVoulue = monthCalendar1.SelectionStart;
            Camping_BDD campingBDD = new Camping_BDD();

            var query =
                        from emplacement in campingBDD.Emplacement
                        where emplacement.TypeEmp == type
                        select new
                        {
                            emplacement.IDEmplacement,
                            emplacement.Occupation,
                            emplacement.TypeEmp
                        };

            var query2 =
                        from reservation in campingBDD.Reservation
                        select new
                        {
                            reservation.IDReservation,
                            reservation.IDEmplacement,
                            reservation.DateDebut,
                            reservation.DateFinApprox,
                            reservation.DateFinDef,
                            reservation.PrixTotal
                        };

            TimeSpan diff = new TimeSpan();
            int id = 0;
                    
            foreach (var i in query)
            {
                //MessageBox.Show("Bonjour");
                int estReserve = -1;
                int compt = 0;
                foreach (var j in query2)
                {
                    //MessageBox.Show("coucou");
                    compt++;
                    if (i.IDEmplacement == j.IDEmplacement)
                    {
                        estReserve = compt;
                        id = j.IDReservation;
                    }
                }
                if (estReserve != -1)
                {
                    foreach (var j in query2)
                    {
                        if (j.IDReservation == id)
                        {
                            if (j.PrixTotal == 0)
                            {
                                diff = j.DateFinApprox.Value.Date - dateVoulue.Date;
                                int diffInt = Convert.ToInt32(diff.TotalDays);
                                if (diffInt > 0)
                                {
                                    dataGridView1.Rows.Add(i.IDEmplacement,diffInt);
                                }
                                else
                                {
                                    diff = j.DateFinDef.Value.Date - dateVoulue.Date;
                                    int diffInt2 = Convert.ToInt32(diff.TotalDays);
                                    if (diffInt2 > 0)
                                    {
                                        dataGridView1.Rows.Add(i.IDEmplacement,diffInt2);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                        {
                            dataGridView1.Rows.Add(i.IDEmplacement, "Pas réservé");
                            //MessageBox.Show("Coucouuuuu");
                        }
         }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            Camping_BDD bdd = new Camping_BDD();
            int ligne = dataGridView1.SelectedCells[0].RowIndex;
            int id = (int)dataGridView1["IDEmplacement", ligne].Value;
            identifiant = id;
            MessageBox.Show("Emplacement n° " + id + " sélectionné", "Selection", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }


        public int getIdentifiant()
        {
            return identifiant;
        }

        private void FormDates_Load(object sender, EventArgs e)
        {

        }

    }
}
